<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">

        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.diseaharmfactors_delete"
                   @click="handleDelete">删 除
        </el-button>


        <el-button
          type="warning"
          size="small"
          plain
          icon="el-icon-download"
          @click="handleExport"
        >
          导 出
        </el-button>
      </template>

    </avue-crud>

  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/basedata/occupationalhazards/diseaharmfactors";
  import {mapGetters} from "vuex";
  import { getDeptTree } from "@/api/system/dept";
  import {getToken} from "@/util/auth";

  const tenantId = JSON.parse(localStorage.getItem("saber-tenantId")).content;

  export default {
    data() {

      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchSpan: 8,
          searchLabelWidth:150,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          dialogClickModal: false,
          column: [
            {
              label: "职业病危害因素类别",
              prop: "zybwgyslb",
              align: "center",
              row: false,
              search:true,
              type:'select',
              dicUrl: "/api/occupationalHazards/diseadict/listNoPage?dictType=1",
              props: {
                label: "dictVal",
                value: "id"
              },
              cascaderItem:  ['zybwhysmc'],
              rules: [{
                required: true,
                message: "请输入职业病危害因素类别",
                trigger: "blur"
              }]
            },
            {
              label: "职业病危害因素名称",
              prop: "zybwhysmc",
              align: "center",
              row: false,
              search:true,
              type:'select',
              dicUrl: "/api/occupationalHazards/diseadict/listNoPage?dictType=2&parentId={{key}}",
              props: {
                label: "dictVal",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入职业病危害因素名称",
                trigger: "blur"
              }]
            },
            {
              label: "危害程度",
              prop: "whcd",
              align: "center",
              row: false,
              search:true,
              type:'select',
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=the_level_of_danger",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              span: 24,
              rules: [{
                required: true,
                message: "请输入危害程度",
                trigger: "blur"
              }]
            },
            {
              label: "可能导致的职业病",
              prop: "kndzdzyb",
              align: "center",
              row: false,
              search:true,
              parent: false,
              type:'tree',
              span: 24,
              dicUrl: "/api/occupationalHazards/diseadict/occupationalDiseaseTreeThatMayCause",
              props: {
                label: "dictVal",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请输入可能导致的职业病",
                trigger: "blur"
              }]
            },
            {
              label: "存在部门",
              prop: "czbm",
              search:true,
              type: "tree",
              align: "center",
              checkStrictly: true,
              dicData: [],
              props: {
                label: "title"
              },
              rules: [{
                required: true,
                message: "请输入存在部门",
                trigger: "blur"
              }]
            },
            {
              label: "存在岗位",
              prop: "czgw",
              align: "center",
              row: false,
              search:true,
              type: "select",
              dicUrl: "/api/blade-system/post/select?tenantId="+tenantId,
              props: {
                label: "postName",
                value: "id",
              },
              rules: [{
                required: true,
                message: "请输入存在岗位",
                trigger: "blur"
              }]
            },
            {
              label: "接触名单",
              prop: "jcmd",
              align: "center",
              hide: true,
              span: 24,
              type: "select",
              multiple: true,
              dicUrl: "/api/major/getUserList",
              props: {
                value: "realName"
              },
            },
            {
              label: "备注",
              prop: "bz",
              align: "center",
              row: false,
              hide: true,
              type: "textarea",
              span: 24,
              maxlength: 150,
              rules: [{
                message: "请输入备注",
                trigger: "blur",

              }]
            },
            {
              label: "企业",
              prop: "bladeDeptId",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "创建人",
              prop: "createUser",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "创建部门",
              prop: "createDept",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "创建时间",
              prop: "createTime",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "修改人",
              prop: "updateUser",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
            {
              label: "修改时间",
              prop: "updateTime",
              align: "center",
              row: false,
              hide: true,
              addDisplay: false,
              editDisplay: false,
              viewDisplay: false,
            },
          ]
        },
        data: []
      };
    },
    mounted() {
      this.initData();
    },
    computed: {
      ...mapGetters(["permission"]),

      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.diseaharmfactors_add, false),
          viewBtn: this.vaildData(this.permission.diseaharmfactors_view, false),
          delBtn: this.vaildData(this.permission.diseaharmfactors_delete, false),
          editBtn: this.vaildData(this.permission.diseaharmfactors_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      initData() {
        // tenantId = JSON.parse(localStorage.getItem("saber-tenantId")).content;
        getDeptTree(tenantId).then(res => {
          const czbmColumn = this.findObject(this.option.column, "czbm");
          czbmColumn.dicData = res.data.data;
          console.log(czbmColumn.dicData);
        }).then(() => {
          this.onLoad(this.page);
        });
      },

      handleExport() {
        this.$confirm("是否导出职业病危害因素信息?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          window.open(`/api/occupationalHazards/diseaharmfactors/export?${this.website.tokenHeader}=${getToken()}`);
        });
      },

      rowSave(row, done, loading) {
        row.jcmd=row.jcmd.join(",");
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        if (typeof(row.jcmd) === 'string'?false:true){
          row.jcmd=row.jcmd.join(",");
        }
        console.log(typeof row.jcmd)
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
